package com.coocaa.x.app.hall.parser;

import com.coocaa.x.framework.data.JObject;
import com.coocaa.x.app.hall.parser.ParserData.Action_Node.Action_Start_App;
import com.coocaa.x.app.hall.parser.ParserData.View_Node.View_title_Node;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lu on 15-6-16.
 */
public class ParserData {

    public static class Root_Node extends JObject {
        public String view, layout, action, info;

        public final void setViewNode(View_Node node) {
            view = node.toJSONString();
        }

        public final void setLayoutNode(Layout_Node node) {
            layout = node.toJSONString();
        }

        public final void setActionNode(Action_Node node) {
            action = node.toJSONString();
        }

        public final void setInfoNode(AppInfo_Node node) {
            info = node.toJSONString();
        }
    }

    public static class NVNode extends JObject {
        public String value_name, value;

        //        @JSONField(serialize = false)
        private String name;

        public NVNode() {

        }

        public NVNode(String name) {
            this.name = name;
        }

        private final String getName() {
            return name;
        }

        public final String getValueName() {
            return value_name;
        }

        public final void setChild(NVNode child) {
            value_name = child.getName();
            value = child.toJSONString();
        }
    }

    public static class View_Node extends JObject {
        public static class View_bg_Node extends JObject {
            public static class BG_Standard extends JObject {
                public String image_url;
            }

            public static class BG_Gallery extends JObject {
                public List<String> image_list;
                public int interval;
            }

            public String bg_standard, bg_gallery;
        }

        public static class View_title_Node extends JObject {
            public static class Title_Standard extends JObject {
                public String title;
            }

            public String title_standard;
        }

        public static class View_tip_Node extends JObject {
            public static class Tip_Standard extends JObject {
                public String tip_url;
            }

            public String tip_standard;
        }

        public String bg, title, tip;
    }

    public static class Layout_Node extends NVNode {
        public static class Layout_Hall extends NVNode {
            public String layout;

            public Layout_Hall() {
                super("layout_hall");
            }

        }
    }

    public static class Action_Node extends NVNode {
        public static class Action_Start_AppTopic extends NVNode {
            public static class Action_Start_AppTopic_ByID extends NVNode {
                public String id;

                public Action_Start_AppTopic_ByID() {
                    super("by_id");
                }

            }

            public Action_Start_AppTopic() {
                super("start_apptopic");
            }

        }

        public static class Action_Start_Web extends NVNode {
            public static class Action_Start_Web_ByUrl extends NVNode {
                public String url;

                public Action_Start_Web_ByUrl() {
                    super("by_url");
                }
            }

            public Action_Start_Web() {
                super("start_web");
            }
        }

        public static class Action_Start_Video extends NVNode {
            public static class Action_Start_Video_ByUrl extends NVNode {
                public String name;
                public String url;

                public Action_Start_Video_ByUrl() {
                    super("by_url");
                }
            }

            public Action_Start_Video() {
                super("start_video");
            }
        }

        public static class Action_Start_AppList extends NVNode {
            public static class Action_Start_AppList_ByID extends NVNode {
                public String id;

                public Action_Start_AppList_ByID() {
                    super("by_id");
                }
            }

            public Action_Start_AppList() {
                super("start_applist");
            }
        }

        public static class Action_Start_App extends NVNode {
            public static class Action_Start_App_ByActivity extends NVNode {
                public String activity;

                public Action_Start_App_ByActivity() {
                    super("by_activity");
                }
            }

            public static class Action_Start_App_ByAction extends NVNode {
                public String action;

                public Action_Start_App_ByAction() {
                    super("by_action");
                }
            }

            public static class Action_Start_App_ByURI extends NVNode {
                public String uri;

                public Action_Start_App_ByURI() {
                    super("by_uri");
                }
            }

            public static class ExtraData extends JObject {
                public List<ExtraItem> extraData;

                public static class ExtraItem extends JObject {
                    public String key;
                    public String value;
                }
            }

            public String pkg;
            public String data;
            public Map<String, String> params = new HashMap<String, String>();

            public Action_Start_App() {
                super("start_app");
            }
        }

        public String content;
    }

    public static class AppInfo_Node extends NVNode {
        public static class App_Info extends JObject {
            public String id;
            public String name;
            public String lmg;
            public String downloads;
            public String softwaresize;
            public String packagename;
            public String version;
        }
        public String appInfo;
    }

    //test


    public static void main(String[] args) {
        View_Node view = new View_Node();
        view.bg = createBG2();
        view.title = createTitle();
        view.tip = createTip();

        Layout_Node layout = new Layout_Node();
        layout.setChild(createLayout_Hall());

        Action_Node action = new Action_Node();
        action.setChild(createAction());

        Root_Node root = new Root_Node();
        root.setViewNode(view);
        root.setLayoutNode(layout);
        root.setActionNode(action);

//        System.out.println(root.toJSONString());

        testParse();
    }

    private static NVNode createAction() {
        Action_Start_App start_app = new Action_Start_App();
        start_app.pkg = "com.tianci.appstore";
        // start_app.params.put("ww", "123");
        // start_app.params.put("uyt", "true");

        Action_Start_App.Action_Start_App_ByActivity by_activity = new Action_Start_App.Action_Start_App_ByActivity();
        by_activity.activity = "com.tianci.appstore.MainActivity";

        Action_Start_App.ExtraData data = new Action_Start_App.ExtraData();
        List<Action_Start_App.ExtraData.ExtraItem> list = new ArrayList<Action_Start_App.ExtraData.ExtraItem>();
        for (int i = 0; i < 3; i++) {
            Action_Start_App.ExtraData.ExtraItem item = new Action_Start_App.ExtraData.ExtraItem();
            item.key = "kk" + i;
            item.value = "vv" + i;
            list.add(item);
        }
        data.extraData = list;
        start_app.data = data.toJSONString();

        start_app.setChild(by_activity);
        return start_app;
    }

    private static NVNode createLayout_Hall() {
        Layout_Node.Layout_Hall hall = new Layout_Node.Layout_Hall();
        hall.layout = "4x2";
        return hall;
    }

    private static String createTip() {
        View_Node.View_tip_Node.Tip_Standard standard = new View_Node.View_tip_Node.Tip_Standard();
        standard.tip_url = "http://www.baidu.com";

        View_Node.View_tip_Node tip = new View_Node.View_tip_Node();
        tip.tip_standard = standard.toJSONString();
        return tip.toJSONString();
    }

    private static String createTitle() {
        View_title_Node.Title_Standard standard = new View_title_Node.Title_Standard();
        standard.title = "测试title";

        View_title_Node title = new View_title_Node();
        title.title_standard = standard.toJSONString();
        return title.toJSONString();
    }

    private static String createBG() {
        View_Node.View_bg_Node.BG_Standard standard = new View_Node.View_bg_Node.BG_Standard();
        standard.image_url = "http://www.163.com";

        View_Node.View_bg_Node bg = new View_Node.View_bg_Node();
        bg.bg_standard = standard.toJSONString();
        return bg.toJSONString();
    }

    private static String createBG2() {
        View_Node.View_bg_Node.BG_Gallery gallery = new View_Node.View_bg_Node.BG_Gallery();
        List<String> list = new ArrayList<String>();
        list.add("111");
        list.add("222");
        list.add("333");
        gallery.image_list = list;

        View_Node.View_bg_Node bg = new View_Node.View_bg_Node();
        bg.bg_gallery = gallery.toJSONString();
        return bg.toJSONString();
    }

    private static void testParse() {
        String str = getJson();
        Root_Node root = Root_Node.parseJObject(str, Root_Node.class);
        System.out.println("  root.action:" + root.action);
        Action_Node action_node = Action_Node.parseJObject(root.action, Action_Node.class);
        String name = action_node.value_name;
        if (name.equals("start_app")) {
            Action_Start_App start_app = Action_Start_App.parseJObject(action_node.value, Action_Start_App.class);
            String pkg = start_app.pkg;
            String data = start_app.data;
            System.out.println(pkg + "  data:" + data);
            Action_Start_App.ExtraData mM = Action_Start_App.ExtraData.parseJObject(data, Action_Start_App.ExtraData.class);
            List<Action_Start_App.ExtraData.ExtraItem> list = mM.extraData;
            System.out.println(list.size());
            for (Action_Start_App.ExtraData.ExtraItem item : list) {
                System.out.println(item.key);
            }
        }

    }

    private static String getJson() {
        String str = "{\"action\":\"{\\\"value\\\":\\\"{\\\\\\\"data\\\\\\\":\\\\\\\"{\\\\\\\\\\\\\\\"extraData\\\\\\\\\\\\\\\":[{\\\\\\\\\\\\\\\"key\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"kk0\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\"value\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"vv0\\\\\\\\\\\\\\\"},{\\\\\\\\\\\\\\\"key\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"kk1\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\"value\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"vv1\\\\\\\\\\\\\\\"},{\\\\\\\\\\\\\\\"key\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"kk2\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\"value\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"vv2\\\\\\\\\\\\\\\"}]}\\\\\\\",\\\\\\\"params\\\\\\\":{},\\\\\\\"pkg\\\\\\\":\\\\\\\"com.tianci.appstore\\\\\\\",\\\\\\\"value\\\\\\\":\\\\\\\"{\\\\\\\\\\\\\\\"activity\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"com.tianci.appstore.MainActivity\\\\\\\\\\\\\\\"}\\\\\\\",\\\\\\\"valueName\\\\\\\":\\\\\\\"by_activity\\\\\\\",\\\\\\\"value_name\\\\\\\":\\\\\\\"by_activity\\\\\\\"}\\\",\\\"valueName\\\":\\\"start_app\\\",\\\"value_name\\\":\\\"start_app\\\"}\",\"layout\":\"{\\\"value\\\":\\\"{\\\\\\\"layout\\\\\\\":\\\\\\\"4x2\\\\\\\"}\\\",\\\"valueName\\\":\\\"layout_hall\\\",\\\"value_name\\\":\\\"layout_hall\\\"}\",\"view\":\"{\\\"bg\\\":\\\"{\\\\\\\"bg_gallery\\\\\\\":\\\\\\\"{\\\\\\\\\\\\\\\"image_list\\\\\\\\\\\\\\\":[\\\\\\\\\\\\\\\"111\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\"222\\\\\\\\\\\\\\\",\\\\\\\\\\\\\\\"333\\\\\\\\\\\\\\\"],\\\\\\\\\\\\\\\"interval\\\\\\\\\\\\\\\":0}\\\\\\\"}\\\",\\\"tip\\\":\\\"{\\\\\\\"tip_standard\\\\\\\":\\\\\\\"{\\\\\\\\\\\\\\\"tip_url\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"http://www.baidu.com\\\\\\\\\\\\\\\"}\\\\\\\"}\\\",\\\"title\\\":\\\"{\\\\\\\"title_standard\\\\\\\":\\\\\\\"{\\\\\\\\\\\\\\\"title\\\\\\\\\\\\\\\":\\\\\\\\\\\\\\\"测试title\\\\\\\\\\\\\\\"}\\\\\\\"}\\\"}\"}\n";
        return str;
    }
}
